//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartBubbleView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartBubbleView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartBubbleView

IMPLEMENT_DYNCREATE(CChartBubbleView, CBCGPChartExampleView)

CChartBubbleView::CChartBubbleView()
	: CBCGPChartExampleView(CChartBubbleView::IDD)
{
	//{{AFX_DATA_INIT(CChartBubbleView)
	m_nTransparency = 30;
	m_nMarkerShape = 0;
	m_bShowDataLabels = TRUE;
	m_nDataLabelAngle = 2;
	m_nScale = 300;
	//}}AFX_DATA_INIT
}

CChartBubbleView::~CChartBubbleView()
{
}

void CChartBubbleView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartBubbleView)
	DDX_Control(pDX, IDC_SCALE, m_wndScale);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	DDX_Control(pDX, IDC_DATA_LABEL_ANGLE, m_wndDataLabelAngle);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SHAPE, m_nMarkerShape);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_ANGLE, m_nDataLabelAngle);
	DDX_Slider(pDX, IDC_SCALE, m_nScale);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartBubbleView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartBubbleView)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_ANGLE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SHAPE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartBubbleView diagnostics

#ifdef _DEBUG
void CChartBubbleView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartBubbleView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartBubbleView message handlers

void CChartBubbleView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	m_wndTransparency.SetRange(0, 100, TRUE);
	m_wndScale.SetRange(25, 300, TRUE);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartBubble);

	pChart->SetChartTitle(_T("Site visitors from US:"));

	pChart->AddChartDataYXY1(_T("California"), 3, 2, 6700);
	pChart->AddChartDataYXY1(_T("Virginia"), 4, 3, 5500);
	pChart->AddChartDataYXY1(_T("New York"), 5, 4, 3200);
	pChart->AddChartDataYXY1(_T("Pennsylvania"), 6, 5, 2500);
	pChart->AddChartDataYXY1(_T("Georgia"), 5, 6, 1400);
	pChart->AddChartDataYXY1(_T("Colorado"), 4, 7, 1350);
	pChart->AddChartDataYXY1(_T("Other States"), 8, 7.5, 1700);

	CBCGPChartBubbleSeries* pBubbleSeries = DYNAMIC_DOWNCAST(CBCGPChartBubbleSeries, pChart->GetSeries(0));
	if (pBubbleSeries != NULL)
	{
		ASSERT_VALID(pBubbleSeries);

		pBubbleSeries->SetDefaultFillGradientType(m_Feature == CDemoFeature::BCGP_Bubble3D ? 
			CBCGPBrush::BCGP_GRADIENT_RADIAL_TOP_LEFT : CBCGPBrush::BCGP_NO_GRADIENT);

		pBubbleSeries->EnableAutoColorDataPoints();

		pBubbleSeries->m_bIncludeDataPointLabelsToLegend = TRUE;
		pBubbleSeries->SetDataLabelContent(BCGPChartDataLabelOptions::LC_BUBBLE_SIZE);
	}

	UpdateData(FALSE);
	OnUpdateChart();
}

void CChartBubbleView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}

void CChartBubbleView::OnUpdateChart()
{
	UpdateData();

	m_wndDataLabelAngle.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartBubbleSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartBubbleSeries, pChart->GetSeries(0));
	if (pSeries != NULL)
	{
		ASSERT_VALID(pSeries);

		pSeries->SetMarkerShape((BCGPChartMarkerOptions::MarkerShape)m_nMarkerShape);
		pSeries->SetBubbleScale(m_nScale);
	}

	double dblDataLabelAngle = (m_nDataLabelAngle - 2) * 45;

	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, FALSE, dblDataLabelAngle);

	pChart->SetThemeOpacity(100 - m_nTransparency);

	UpdateBubbleBrushes();
}

void CChartBubbleView::UpdateChartColorTheme(int nTheme, BOOL bIsDarkTheme)
{
	CBCGPChartExampleView::UpdateChartColorTheme(nTheme, bIsDarkTheme);
	UpdateBubbleBrushes();
}

void CChartBubbleView::UpdateBubbleBrushes()
{
	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeries = pChart->GetSeries(0);
	if (pSeries != NULL && m_Feature == CDemoFeature::BCGP_Bubble3D)
	{
		ASSERT_VALID(pSeries);

		pChart->SetSeriesShadow();
		
		pSeries->ClearDataPointFormat();
		
		for (int i = 0; i < pSeries->GetDataPointCount(); i++)
		{
			BCGPSeriesColorsPtr colors;
			const BCGPChartFormatSeries* pFormatSeries = pSeries->GetColors(colors, i);
			
			if (pFormatSeries != NULL)
			{
				BCGPChartFormatSeries* pFormat = (BCGPChartFormatSeries*)pSeries->GetDataPointFormat(i, TRUE);
				pFormat->m_seriesElementFormat.m_brFillColor = CBCGPBrush(
					colors.m_pBrElementFillColor->GetColor(), CBCGPColor::White, CBCGPBrush::BCGP_GRADIENT_RADIAL_TOP_LEFT);
			}
		}
	}

	pChart->Redraw();
}
